import { Injectable, UnauthorizedException } from '@nestjs/common';
// import { InjectRepository } from '@nestjs/typeorm';
// import { Repository } from 'typeorm';
// import { User } from '../user/entities/user.entity';
import { UserService } from 'src/user/user.service';
import * as bcrypt from 'bcryptjs';
export interface LoginDto {
  username: string;
  password: string;
}
@Injectable()
export class LoginService {
  constructor(
    private readonly userService: UserService, // 注入 UserService
  ) {}
  async login(loginDto: LoginDto) {
    const { username, password } = loginDto;
    const user = await this.userService.userRepository.findOne({
      where: { username },
      select: ['id', 'username', 'password'],
    });
    console.log(user);
    if (!user) {
      throw new UnauthorizedException('用户不存在');
    }
    const isPasswordValid = password == user.password ? true : false;
    if (!isPasswordValid) {
      throw new UnauthorizedException('密码错误');
    }
    return '登录成功';
  }
}
